package com.javastudy.w2.tools;

import com.javastudy.w2.model.ResponseModel;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
/**
 * OKhttp帮助类
 *
 * @author henry
 * @date 2021/11/13
 */
public class OkHttpUtils {


    public static ResponseModel get(String url) {
        ResponseModel responseResult= new ResponseModel();
        OkHttpClient client = new OkHttpClient();
        //构造request
        Request request = new Request.Builder().url(url).build();
        try (Response response = client.newCall(request).execute()) {
            responseResult.setResponseContent(response.body().string());
            responseResult.setSuccess(true);
        }
        catch (Exception ex){
            responseResult.setMessage("获取请求失败了，错误原因：\n"+ex.getMessage());
        }
        return responseResult;
    }
}
